//package tasko.storage;
import static org.junit.Assert.*;
//import tasko.main.TaskoConstants;
//import tasko.main.Logger;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import org.junit.Test;


public class FileReadWrite {

	private final static Logger logger = new Logger(TaskoConstants.FILE_NAME_LOGGER);

	public void writeLine(String fileName, String contentLine) {
		try {
			FileWriter fstream = new FileWriter(fileName,true);
			BufferedWriter out = new BufferedWriter(fstream);
			out.write(contentLine + "\n");
			out.close();
		} catch (IOException e) {
			logger.logEventError(TaskoConstants.ERROR_WRITING_FILE);
		}
	}
	/**
	 * Reads the first line from file
	 * @return
	 */
	public String readLine(String fileName) {
		String s = "";
		try {
			BufferedReader br = new BufferedReader(new FileReader(fileName));
			if((s = br.readLine()) == null){
				s = "";
			}
			br.close();
		} catch (FileNotFoundException ex) {
			logger.logEventError(ex.getMessage());
		} catch (IOException e) {
			logger.logEventError(e.getMessage());
		}
		return s;
	}
	/**
	 * Reads all the lines from the file, and return it in an Vector.
	 * @return
	 */
	public Vector<String> readAll(String fileName) {
		FileReader fileReader;
		Vector<String> fileList = new Vector<String>();
		String strLine;
		try {
			fileReader = new FileReader(fileName);
			BufferedReader bufferedReader = new BufferedReader(fileReader);

			while((strLine = bufferedReader.readLine()) != null) {
				fileList.add(strLine);
			}
			fileReader.close();
			bufferedReader.close();
		} catch (IOException e) {
			logger.logEventError(TaskoConstants.ERROR_READING_FILE);
		}
		return fileList;
	}

	/**
	 * Clears all the existing file content and writes the list of tasks to the file
	 * @param tasks
	 * @return
	 */
	public boolean writeAllTasks(String fileName, List<String> tasks) {
		deleteOldCreateNewFile(fileName);
		return writeToFile(fileName, tasks);
	}

	public boolean writeNewTasks(String fileName, List<String> tasks) {
		return writeToFile(fileName, tasks);
	}
	
	/**
	 * @param tasks
	 * @return
	 */
	private boolean writeToFile(String fileName, List<String> tasks) {
		try {
			FileWriter fstream = new FileWriter(fileName,true);
			BufferedWriter out = new BufferedWriter(fstream);
			for (String task : tasks) {
				out.write(task + "\n");
			}
			out.close();
		} catch (IOException e) {
			logger.logEventError(TaskoConstants.ERROR_WRITING_FILE);
			return false;
		}
		return true;
	}
	
	
	private void deleteOldCreateNewFile(String inputFileName) {
		File newFile = new File(inputFileName);
		if(newFile.exists()){
			newFile.delete();
		}
		try {
			newFile.createNewFile();
		} catch (IOException e) {
			logger.logEventError(TaskoConstants.ERROR_CLEAR_FILE);
		}
	}
	
	@Test
	public void testFileReadWrite(){
		String fileName = "test.txt";
		//Start with an empty file
		this.deleteOldCreateNewFile(fileName);
		this.writeLine(fileName, "Hello how are you?");
		this.writeLine(fileName, "line number 2");
		for (String s : this.readAll(fileName)) {
			System.out.println(s);
		}
		assertEquals(this.readLine(fileName), "Hello how are you?");
		Vector<String> list = new Vector<String>();
		list.add("hello how are you?");
		list.add("line number 2");
		for (String s : list) {
			System.out.println(s);
		}
		assertFalse(this.readAll(fileName).elementAt(0).equals(list.elementAt(0)));
		list.set(0, "Hello how are you?");
		assertEquals(this.readAll(fileName).elementAt(0), list.elementAt(0));
		assertEquals(this.readAll(fileName).elementAt(1), list.elementAt(1));
		this.writeAllTasks(fileName, list);
		assertEquals(this.readAll(fileName).elementAt(0), list.elementAt(0));
		assertEquals(this.readAll(fileName).elementAt(1), list.elementAt(1));
		list.set(0, "New Hello how are you?");
		list.set(1, "New line number 2?");
		this.writeNewTasks(fileName, list);
		assertEquals(this.readAll(fileName).elementAt(2), list.elementAt(0));
		assertEquals(this.readAll(fileName).elementAt(3), list.elementAt(1));
	}

}
